using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data;
using System.Text;

public partial class Pages_AccountStockNew : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button_acctype_Click(object sender, EventArgs e)
    {
        Label3.Text = "";
        if (!CheckBox1.Checked)
        {
             Label3.Text = "First confirm by ticking the switch";
             return;
        }
        if (!(string.IsNullOrEmpty(TextBox1.Text.Trim())))
        { 
            CheckBox1.Checked = false;
            int newcode=0;
            DataSet ds = new DataSet();
            Common.Getdata(ds, "select DESCRIPT,CODE from TYPE WHERE VARFIELD='ACCTYPE' and DESCRIPT='" + TextBox1.Text.Trim() + "'", "type");
            // Check if duplicated
            if (ds.Tables["type"].Rows.Count > 0)
            {
                Label3.Text = "account type already exists";
            }
            // else find the new code for it
            else
            {
                Common.Getdata(ds, "select max(code) from type where varfield ='ACCTYPE'", "maxcode");
                newcode=Convert.ToInt32(ds.Tables["maxcode"].Rows[0][0]) + 1;
                Label3.Text= TextBox1.Text + newcode.ToString();
                // Save into table type
 
                string conString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
                //string conString = @"Provider=SQLSql;Data Source=CSE-PC\SQLEXPRESS;User ID=sa;Initial Catalog=DB2011;Password=1234";
                SqlCommand myCommInsert = new SqlCommand();
                myCommInsert.Connection = new SqlConnection(conString);
                myCommInsert.Connection.Open();
                string strQuery = "INSERT INTO [DB2011].[dbo].[TYPE] ([VARFIELD],[DESCRIPT],[CODE]) VALUES('ACCTYPE','"+
                    TextBox1.Text.Trim() +"','"+newcode+"')";
                myCommInsert.CommandText = strQuery;
                myCommInsert.ExecuteNonQuery();
                myCommInsert.Connection.Close();
                TextBox1.Text = "";
            }
        }
    }
    protected void Button_acccode_Click(object sender, EventArgs e)
    {
        
        Label3.Text = "";
        if (!CheckBox1.Checked)
        {
            Label3.Text = "First confirm by ticking the switch";
            return;
        }
        if (!(string.IsNullOrEmpty(TextBox1.Text.Trim())))
        {
            CheckBox1.Checked = false;
            int newcode = 0;
            DataSet ds = new DataSet();
            Common.Getdata(ds, "select * from ACCOUNT WHERE ACCTYPE='"+Common.accTypeValue+"' and ACCNAME='" + TextBox1.Text.Trim() + "'", "tbl");
            //Common.Getdata(ds, "select * from ACCOUNT WHERE ACCTYPE=3, and ACCNAME='" + TextBox1.Text.Trim() + "'", "tbl");
            // Check if duplicated
            if (ds.Tables["tbl"].Rows.Count > 0)
            {
                Label3.Text = "account already exists";
            }

            else    // else find the new code for it
            {
                Common.Getdata(ds, "select max(acccode) from account where ACCTYPE='"+Common.accTypeValue+"'", "maxcode");
                newcode = Convert.ToInt32(ds.Tables["maxcode"].Rows[0][0]) + 1;
                Label3.Text = TextBox1.Text + newcode.ToString();
                
                // Save into table type

                string conString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
                //string conString = @"Provider=SQLSql;Data Source=CSE-PC\SQLEXPRESS;User ID=sa;Initial Catalog=DB2011;Password=1234";
                SqlCommand myCommInsert = new SqlCommand();
                myCommInsert.Connection = new SqlConnection(conString);
                myCommInsert.Connection.Open();
                //string strQuery = "INSERT INTO [DB2011].[dbo].[ACCOUNT] ([ACCCODE],[ACCTYPE],[ACCNAME])" +
                //    " VALUES('" + newcode + "','" + Common.accTypeValue + "','" + TextBox1.Text.Trim() + "')";
                string strQuery = "INSERT INTO [DB2011].[dbo].[ACCOUNT] ([ACCCODE],[ACCTYPE],[ACCNAME],[BALANCE],[OPEN_DATE])" +
                    " VALUES('" + newcode + "','" + Common.accTypeValue + "','" +
                     TextBox1.Text.Trim() + "','0','" + DateTime.Today + "')";
                myCommInsert.CommandText = strQuery;
                myCommInsert.ExecuteNonQuery();
                myCommInsert.Connection.Close();
                TextBox1.Text = "";


            }
        }
    }
    protected void Button_itype_Click(object sender, EventArgs e)
    {
        Label3.Text = "";
        if (!CheckBox1.Checked)
        {
            Label3.Text = "First confirm by ticking the switch";
            return;
        }
        if (!(string.IsNullOrEmpty(TextBox1.Text.Trim())))
        {
            CheckBox1.Checked = false;
            int newcode = 0;
            DataSet ds = new DataSet();
            Common.Getdata(ds, "select DESCRIPT,CODE from TYPE WHERE VARFIELD='ITYPE' and DESCRIPT='" + TextBox1.Text.Trim() + "'", "type");
            // Check if duplicated
            if (ds.Tables["type"].Rows.Count > 0)
            {
                Label3.Text = "STOCK type already exists";
            }
            // else find the new code for it
            else
            {
                Common.Getdata(ds, "select max(code) from type where varfield ='ITYPE'", "maxcode");
                newcode = Convert.ToInt32(ds.Tables["maxcode"].Rows[0][0]) + 1;
                Label3.Text = TextBox1.Text + newcode.ToString();
                // Save into table type

                string conString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
                //string conString = @"Provider=SQLSql;Data Source=CSE-PC\SQLEXPRESS;User ID=sa;Initial Catalog=DB2011;Password=1234";
                SqlCommand myCommInsert = new SqlCommand();
                myCommInsert.Connection = new SqlConnection(conString);
                myCommInsert.Connection.Open();
                string strQuery = "INSERT INTO [DB2011].[dbo].[TYPE] ([VARFIELD],[DESCRIPT],[CODE]) VALUES('ITYPE','" +
                    TextBox1.Text.Trim() + "','" + newcode + "')";
                myCommInsert.CommandText = strQuery;
                myCommInsert.ExecuteNonQuery();
                myCommInsert.Connection.Close();
                TextBox1.Text = "";

            }

        }
    }
    protected void Button_icode_Click(object sender, EventArgs e)
    {

        try
        {
            Label3.Text = "";
            if (!CheckBox1.Checked)
            {
                Label3.Text = "First confirm by ticking the switch";
                return;
            }
            if (!(string.IsNullOrEmpty(TextBox1.Text.Trim())))
            {
                CheckBox1.Checked = false;
                int newcode = 0;
                DataSet ds = new DataSet();
                Common.Getdata(ds, "select * from STOCK WHERE ITYPE='" + Common.stkTypeValue + "' and INAME='" + TextBox1.Text.Trim() + "'", "tbl");
                //Common.Getdata(ds, "select * from STOCK WHERE ITYPE='" + Common.stkTypeValue + "' and INAME='8'' x 3'''", "tbl");
                //Common.Getdata(ds, "select * from ACCOUNT WHERE ACCTYPE=3, and ACCNAME='" + TextBox1.Text.Trim() + "'", "tbl");
                // Check if duplicated
                if (ds.Tables["tbl"].Rows.Count > 0)
                {
                    Label3.Text = "account already exists";
                }

                else    // else find the new code for it
                {
                    Common.Getdata(ds, "select max(icode) from stock where ITYPE='" + Common.stkTypeValue + "'", "maxcode");
                    newcode = Convert.ToInt32(ds.Tables["maxcode"].Rows[0][0]) + 1;
                    Label3.Text = TextBox1.Text + newcode.ToString();
                    //Label3.Text = "inspect";
                    // Save into table type

                    string conString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
                    //string conString = @"Provider=SQLSql;Data Source=CSE-PC\SQLEXPRESS;User ID=sa;Initial Catalog=DB2011;Password=1234";
                    SqlCommand myCommInsert = new SqlCommand();
                    myCommInsert.Connection = new SqlConnection(conString);
                    myCommInsert.Connection.Open();
                    string strQuery = "INSERT INTO [DB2011].[dbo].[STOCK] ([ICODE],[ITYPE],[INAME])" +
                        " VALUES('" + newcode + "','" + Common.stkTypeValue + "','" + TextBox1.Text.Trim() + "')";
                    //string strQuery = "INSERT INTO [DB2011].[dbo].[ACCOUNT] ([ACCCODE],[ACCTYPE],[ACCNAME],[BALANCE],[OPEN_DATE])" +
                    //    " VALUES('" + newcode + "','" + Common.accTypeValue + "','" +
                    //     TextBox1.Text.Trim() + "','0','" + DateTime.Today + "')";
                    myCommInsert.CommandText = strQuery;
                    myCommInsert.ExecuteNonQuery();
                    myCommInsert.Connection.Close();
                    TextBox1.Text = "";


                }
            }
        }
        catch ( Exception Ex)
        {
            Label3.Text = Ex.Message;
            throw;
        }
        
    }
}
